{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e294d0af",
   "metadata": {},
   "source": [
    "# 本周内容：Web应用"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a463a198",
   "metadata": {},
   "source": [
    "## 1. 使用Flask 搭建Web最小应用\n",
    "\n",
    "> 1. 使用select创建一个html页面:index.html\n",
    "> 2. 使用Flsak中的 render_template 实现页面的路由跳转"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "770292eb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: requests-html in e:\\python311\\lib\\site-packages (0.10.0)\n",
      "Requirement already satisfied: requests in e:\\python311\\lib\\site-packages (from requests-html) (2.30.0)\n",
      "Requirement already satisfied: pyquery in e:\\python311\\lib\\site-packages (from requests-html) (2.0.0)\n",
      "Requirement already satisfied: fake-useragent in e:\\python311\\lib\\site-packages (from requests-html) (1.2.1)\n",
      "Requirement already satisfied: parse in e:\\python311\\lib\\site-packages (from requests-html) (1.19.1)\n",
      "Requirement already satisfied: bs4 in e:\\python311\\lib\\site-packages (from requests-html) (0.0.1)\n",
      "Requirement already satisfied: w3lib in e:\\python311\\lib\\site-packages (from requests-html) (2.1.2)\n",
      "Requirement already satisfied: pyppeteer>=0.0.14 in e:\\python311\\lib\\site-packages (from requests-html) (1.0.2)\n",
      "Requirement already satisfied: appdirs<2.0.0,>=1.4.3 in e:\\python311\\lib\\site-packages (from pyppeteer>=0.0.14->requests-html) (1.4.4)\n",
      "Requirement already satisfied: certifi>=2021 in e:\\python311\\lib\\site-packages (from pyppeteer>=0.0.14->requests-html) (2023.5.7)\n",
      "Requirement already satisfied: importlib-metadata>=1.4 in e:\\python311\\lib\\site-packages (from pyppeteer>=0.0.14->requests-html) (6.8.0)\n",
      "Requirement already satisfied: pyee<9.0.0,>=8.1.0 in e:\\python311\\lib\\site-packages (from pyppeteer>=0.0.14->requests-html) (8.2.2)\n",
      "Requirement already satisfied: tqdm<5.0.0,>=4.42.1 in e:\\python311\\lib\\site-packages (from pyppeteer>=0.0.14->requests-html) (4.65.0)\n",
      "Requirement already satisfied: urllib3<2.0.0,>=1.25.8 in e:\\python311\\lib\\site-packages (from pyppeteer>=0.0.14->requests-html) (1.26.16)\n",
      "Requirement already satisfied: websockets<11.0,>=10.0 in e:\\python311\\lib\\site-packages (from pyppeteer>=0.0.14->requests-html) (10.4)\n",
      "Requirement already satisfied: beautifulsoup4 in e:\\python311\\lib\\site-packages (from bs4->requests-html) (4.11.2)\n",
      "Requirement already satisfied: lxml>=2.1 in e:\\python311\\lib\\site-packages (from pyquery->requests-html) (4.9.2)\n",
      "Requirement already satisfied: cssselect>=1.2.0 in e:\\python311\\lib\\site-packages (from pyquery->requests-html) (1.2.0)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in e:\\python311\\lib\\site-packages (from requests->requests-html) (3.1.0)\n",
      "Requirement already satisfied: idna<4,>=2.5 in e:\\python311\\lib\\site-packages (from requests->requests-html) (3.4)\n",
      "Requirement already satisfied: zipp>=0.5 in e:\\python311\\lib\\site-packages (from importlib-metadata>=1.4->pyppeteer>=0.0.14->requests-html) (3.16.2)\n",
      "Requirement already satisfied: colorama in e:\\python311\\lib\\site-packages (from tqdm<5.0.0,>=4.42.1->pyppeteer>=0.0.14->requests-html) (0.4.6)\n",
      "Requirement already satisfied: soupsieve>1.2 in e:\\python311\\lib\\site-packages (from beautifulsoup4->bs4->requests-html) (2.4)\n"
     ]
    }
   ],
   "source": [
    "!pip install requests-html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bb06db44",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * Serving Flask app '__main__'\n",
      " * Debug mode: off\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.\n",
      " * Running on http://127.0.0.1:5000\n",
      "Press CTRL+C to quit\n"
     ]
    }
   ],
   "source": [
    "from flask import Flask\n",
    "from flask import render_template\n",
    "\n",
    "\n",
    "app = Flask(__name__)\n",
    "\n",
    "@app.route('/')\n",
    "def hello_world():\n",
    "    return render_template('index.html')\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    app.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ecd49c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "from flask import Flask\n",
    "from flask import render_template,request\n",
    "import crawl_liepin\n",
    "from jinja2 import Markup, Environment, FileSystemLoader\n",
    "from pyecharts.globals import CurrentConfig\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Map\n",
    "\n",
    "# 关于 CurrentConfig，可参考 [基本使用-全局变量]\n",
    "CurrentConfig.GLOBAL_ENV = Environment(loader=FileSystemLoader(\"./templates\"))\n",
    "\n",
    "app = Flask(__name__)\n",
    "\n",
    "@app.route('/')\n",
    "def index():\n",
    "    return render_template(\"index.html\")\n",
    "\n",
    "@app.route('/user_choose',methods = ['POST', 'GET'])\n",
    "def user_choose():\n",
    "    print(request.form['city'])\n",
    "    print(request.form['category'])\n",
    "    print(request.form['workexp'])\n",
    "    user_city = request.form['city']\n",
    "    user_category = request.form['category']\n",
    "    user_workexp = request.form['workexp']\n",
    "    crawl_results = crawl_liepin.crawl(城市 = user_city ,关键词=user_category,工作经验=user_workexp)\n",
    "    crawl_results_df = crawl_results[\n",
    "        [\n",
    "            'recruiter.recruiterTitle',\n",
    "            'comp.compScale',\n",
    "            'comp.compStage',\n",
    "            'comp.compIndustry',\n",
    "            'comp.compName',\n",
    "            'job.labels',\n",
    "            'job.dq',\n",
    "            'job.campusJobKind',\n",
    "            'job.title',\n",
    "            'job.salary'\n",
    "        ]\n",
    "    ]\n",
    "    crawl_results_df.to_excel('%s_%s_%s.xlsx'%(user_city,user_category,user_workexp))\n",
    "    crawl_results_html = crawl_results_df.to_html()\n",
    "    # 文件存储\n",
    "    text_file = open(\"templates/results.html\", \"w\",encoding=\"utf-8\")\n",
    "    text_file.write(crawl_results_html)\n",
    "    text_file.close()\n",
    "    return render_template(\"results.html\")\n",
    "\n",
    "def map_base() -> Map:\n",
    "    df = pd.read_excel('北京_产品运营_应届生.xlsx')\n",
    "    series_dq = df['job.dq'].value_counts() # 整理地区的区域数据\n",
    "    dq_name= [i for i in series_dq.index if '-' in i] # 梳理区域名称\n",
    "    series_dq[dq_name].values #　梳理各区域的数据\n",
    "    c = (\n",
    "        Map()\n",
    "        .add(\"产品经理地区分布\", [list(z) for z in zip([i.split('-')[1] for i in dq_name],series_dq[dq_name].values.tolist())], \"北京\")\n",
    "        .set_global_opts(\n",
    "            title_opts=opts.TitleOpts(title=\"Map-北京_产品运营_应届生\"), visualmap_opts=opts.VisualMapOpts()\n",
    "        )\n",
    "\n",
    "    )\n",
    "    \n",
    "    return c\n",
    "\n",
    "\n",
    "@app.route(\"/map\")\n",
    "def map_test():\n",
    "    c = map_base()\n",
    "    return Markup(c.render_embed())                        \n",
    "\n",
    "if __name__ == '__main__':\n",
    "    app.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "953df56b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
